package com.free4inno.knowledgems.utils;

import com.free4inno.knowledgems.dao.ResourceDao;
import com.free4inno.knowledgems.domain.Resource;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.layout.font.FontProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.html2pdf.HtmlConverter;


/**
 * @author : liuxinyuan
 * @date : 10:51 2021/11/23
 */

@Slf4j
@Component
public class PdfUtils {

    @Value("${pdf.temp.path}")
    private String pdfTempPath;

    @Value("${pdf.font.path}")
    private String pdfFontPath;

    @Value("${file.environment}")
    private String fileEnvironment;

    @Autowired
    private ResourceDao resourceDao;

    public String toPdf(int id) throws IOException {
        String tempSavePath = getRealPath(pdfTempPath);

        //判断文件夹是否被创建，未被创建则自动创建
        File folder = new File(tempSavePath);
        if (!folder.exists() && !folder.isDirectory()) {
            folder.mkdirs();
        }

//            // 1.新建document对象
//            Document document = new Document();
//            // 2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
//            // 创建 PdfWriter 对象 第一个参数是对文档对象的引用，第二个参数是文件的实际名称，在该名称中还会给出其输出路径。
//
//
//            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("src/main/webapp/pdf/"+id+".pdf"));
//            // 3.打开文档
//            document.open();
//            BaseFont baseFont = BaseFont.createFont("src/main/webapp/assets/vendor/font/song.ttf",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//            Font font = new Font(baseFont);
        Resource resource = resourceDao.findAllById(id);
//            // 4.添加一个内容段落
//            String str=resource.getText();
//            str=str.replaceAll("<[^>]*>","");
//            str=str.replaceAll("&nbsp"," ");
//            document.add(new Paragraph(str,font));
//            // 5.关闭文档
//            document.close();

        createPdf(resource.getText(), tempSavePath + id + ".pdf");
        return tempSavePath;
    }

    public void createPdf(String html, String dest) throws IOException {
        ConverterProperties props = new ConverterProperties();
        FontProvider fp = new FontProvider();
        fp.addStandardPdfFonts();
        fp.addDirectory(getRealPath(pdfFontPath));
        props.setFontProvider(fp);
        HtmlConverter.convertToPdf(html, new FileOutputStream(dest), props);
    }

    public String getRealPath(String path) {
        // path & OS
        String realPath = "";
        if ( fileEnvironment.equals("dev") ) {
            // get system run path
            String runPath = System.getProperty("user.dir");
            realPath = runPath + path;
        } else if (fileEnvironment.equals("prod")){
            realPath = path;
        }
        return realPath;
    }
}